<?php
/**
 * @author 		: Mauricio Amorim
 * @date		: 05/12/2010
 * @desc		: controller a ser extendida por outra controller para poder manipular a content
 */
include_once "library/facil3/core/controller/interface/HTTPControllerInterface.class.php";
include_once "library/facil3/utils/Navigation.class.php";

include_once("library/facil3/core/modules/category/dao/CategoryDAO.class.php");
include_once("library/facil3/core/modules/category/vo/CategoryVO.class.php");
include_once("library/jogando/modules/jogo/dao/JogoDAO.class.php");

include_once(DataHandler::removeDobleBars(Config::getFolderView("HttpRoot.class.php")));
//para retorno
include_once "library/facil3/core/vo/ReturnResultVO.class.php";

class RootController implements HTTPControllerInterface{

	protected $arrayVariable;
	public $arrayRestFolder;
	
	public function __construct($arrayRestFolder = NULL){
		if($arrayRestFolder != NULL){
			$this->arrayVariable   = Navigation::getVariableArraySlug($arrayRestFolder);
            $this->arrayRestFolder = $arrayRestFolder;
        }
	}

    /*
     * busca todos os higlights da categoria pai e filho
     * @see library/facil3/core/controller/content/GenericFacilContent#init()
     */
    public function init(){
    	//inicia a array final da categoria pai
    	$StdCategoryVO 			= array();
    	$JogoDAO = JogoDAO::getInstance();
    	$page = DataHandler::forceInt(DataHandler::getValueByArrayIndex($this->arrayVariable, "pag"));
    	$page = ($page >= 1)?$page:1;
    	$ResultDAO = $JogoDAO->select(JogoDAO::RETURN_STD_OBJECT, NULL, 1, 
								$category_id = NULL,
								$name = NULL, 
								$ranking = NULL, 
								$order_type = NULL,						
								$quant_limit = 30, 
								$quant_started = ($page-1)*30);
		$lista_ordenada = $ResultDAO->result;
		
    	//Debug::print_r($ResultDAO);
    	$array_random = array();
    	$RandomRestul = $this->getRandomJogos(10);
    	$array_random = $RandomRestul->result;
    	
    	
    	//Debug::print_r($ResultDAO);
    	//exit();
		//iniciando o retorno padrao em http result
		$returnResult = new HttpResult();
		//iniciando o resultado para o html
		$retornoDaPaginaHTML = new HttpRoot();
		
		$retornoDaPaginaHTML->array_random = $array_random;
		$retornoDaPaginaHTML->lista_ordenada = $lista_ordenada;
		
		$retornoDaPaginaHTML->count_total 	= $ResultDAO->count_total;
		$retornoDaPaginaHTML->total_pages 	= floor($ResultDAO->count_total/30)+1;
		$retornoDaPaginaHTML->this_page 	= $page;
		
		$returnResult->setHttpContentResult($retornoDaPaginaHTML);
		//Debug::print_r($returnResult);
		return $returnResult;
    }
	
	private function getRandomJogos($quant, $category_id = NULL){
		$JogoDAO = JogoDAO::getInstance();
		$ReturnRestulJogo = $JogoDAO->select(
								$typeOfReturn = JogoDAO::RETURN_STD_OBJECT, 
								$id = NULL, 
								$active = 1, 
								$category_id,
								$name = NULL, 
								$ranking = NULL, 
								$order_type = JogoDAO::ORDER_RAND,						
								$quant_limit = $quant, 
								$quant_started = 1
							);
		return $ReturnRestulJogo;
	}
}
